package com.screen.screendisplay.service.impl.ne;

import com.screen.screendisplay.entity.ne.NeOpermonEqpState;
import com.screen.screendisplay.mapper.ne.NeOpermonEqpStateMapper;
import com.screen.screendisplay.service.ne.NeOpermonEqpStateService;
import com.screen.screendisplay.vo.ne.NeEquipmentOverviewVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 新能源设备状态监测表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeOpermonEqpStateServiceImpl extends ServiceImpl<NeOpermonEqpStateMapper, NeOpermonEqpState> implements NeOpermonEqpStateService {

    @Override
    public NeEquipmentOverviewVO getEquipmentOverview(Integer year) {
        QueryWrapper<NeOpermonEqpState> queryWrapper = new QueryWrapper<>();
        
        if (year != null) {
            queryWrapper.eq("year", year);
        } else {
            // 如果没有指定年份，获取当前年份的数据
            int currentYear = LocalDate.now().getYear();
            queryWrapper.eq("year", currentYear);
        }
        
        // 按时间降序排列，获取最新的一条记录
        queryWrapper.orderByDesc("year", "month", "day");
        queryWrapper.last("LIMIT 1");
        
        NeOpermonEqpState entity = this.getOne(queryWrapper);
        
        NeEquipmentOverviewVO vo = new NeEquipmentOverviewVO();
        if (entity != null) {
            vo.setWindTurbineTotal(entity.getWindTurbineTotal());
            vo.setWindTurbineNormal(entity.getWindTurbineNormal());
            vo.setPvStationTotal(entity.getPvStationTotal());
            vo.setInverterNormal(entity.getInverterNormal());
        } else {
            // 如果数据库中没有数据，返回默认值（与原mock数据一致）
            vo.setWindTurbineTotal(150);
            vo.setWindTurbineNormal(142);
            vo.setPvStationTotal(85);
            vo.setInverterNormal(78);
        }
        
        return vo;
    }

}
